﻿@using Grand.Business.Core.Interfaces.Common.Localization
@model DocumentListModel
@inject AdminAreaSettings adminAreaSettings
@inject ITranslationService translationService 
@{
    //page title
    ViewBag.Title = Loc["Admin.Documents.Document.Manage"];
    var selectedList = EnumTranslationService.ToSelectList(DocumentStatus.Open, false).ToList();
    selectedList.Insert(0, new SelectListItem { Text = translationService.GetResource("Admin.Common.All"), Value = "-1", Selected = true });
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit popup-window">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-sitemap"></i>
                    @Loc["Admin.Documents.Document.Manage"]
                </div>
                <div class="actions btn-group btn-group-devided util-btn-margin-bottom-5">
                    <a href="@Url.Action("CreateDocument", new { area = Constants.AreaAdmin })" class="btn green">
                        <i class="fa fa-plus"></i><span class="d-none d-sm-inline"> @Loc["Admin.Common.AddNew"] </span>
                    </a>
                    <vc:admin-widget widget-zone="document_list_buttons" additional-data="null"/>
                </div>
            </div>
            <div class="x_content form">
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="main-header col-12 px-0">
                            <div class="row align-items-end">
                                <div class="col-md-4 col-ms-12 col-12">
                                    <div class="form-group mb-0">
                                        <admin-label asp-for="SearchName" class="control-label"/>
                                        <admin-input asp-for="SearchName"/>
                                    </div>
                                </div>
                                <div class="col-md-4 col-ms-12 col-12">
                                    <div class="form-group mb-0">
                                        <admin-label asp-for="SearchNumber" class="control-label"/>
                                        <admin-input asp-for="SearchNumber"/>
                                    </div>
                                </div>
                                <div class="col-md-4 col-sm-12 col-12">
                                    <div class="form-actions">
                                        <div class="btn-group">
                                            <button class="btn btn-success filter-submit" id="search-documents">
                                                <i class="fa fa-search"></i> @Loc["Admin.Common.Search"]
                                            </button>
                                            <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#filterCollapse" aria-expanded="false" aria-controls="filterCollapse">
                                                <i class="fa fa-filter"></i>&nbsp; @Loc["Admin.Common.Filters"]
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="collapse" id="filterCollapse">
                            <div class="drop-filters-container w-100">
                                <div class="form-group">
                                    <admin-label asp-for="SearchEmail" class="col-sm-3 control-label"/>
                                    <div class="col-md-9 col-sm-9">
                                        <admin-input asp-for="SearchEmail"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <admin-label asp-for="StatusId" class="col-sm-3 control-label"/>
                                    <div class="col-md-9 col-sm-9">
                                        @Html.DropDownList("StatusId", selectedList, new { @class = "form-control" })
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="x_content">
                <div id="documents-grid"></div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        $('#StatusId').val("-1");

        $("#documents-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("ListDocuments", "Document", new { area = Constants.AreaAdmin }))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(adminAreaSettings.GridPageSizes)]
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            columns: [{
                field: "Number",
                title: "@Loc["Admin.Documents.Document.Fields.Number"]",
                template: '<a class="k-link" href="EditDocument/#=Id#">#=kendo.htmlEncode(Number)#</a>',
            },
            {
                field: "Name",
                title: "@Loc["Admin.Documents.Document.Fields.Name"]",
                template: '<a class="k-link" href="EditDocument/#=Id#">#=kendo.htmlEncode(Name)#</a>',
            },
            {
                field: "CustomerEmail",
                title: "@Loc["Admin.Documents.Document.Fields.CustomerEmail"]",
                template: '<a class="k-link" href="EditDocument/#=Id#">#=kendo.htmlEncode(CustomerEmail)#</a>',
            },
            {
                field: "Published",
                title: "@Loc["Admin.Documents.Document.Fields.Published"]",
                width: 100,
                minScreenWidth: 500,
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
            }, {
                field: "DisplayOrder",
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                title: "@Loc["Admin.Documents.Document.Fields.DisplayOrder"]",
                width: 100,    
                minScreenWidth: 750,
            }]
        });
    });
</script>
<script>
    $(document).ready(function () {
        //search button
        $('#search-documents').click(function () {
            //search
            var grid = $('#documents-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            return false;
        });

        $("#@Html.IdFor(model => model.SearchName)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-documents").click();
                return false;
            }
        });
        $("#@Html.IdFor(model => model.SearchNumber)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-documents").click();
                return false;
            }
        });
        $("#@Html.IdFor(model => model.SearchEmail)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-documents").click();
                return false;
            }
        });


    });

    function additionalData() {
        var data = {
            SearchName: $('#@Html.IdFor(model => model.SearchName)').val(),
            SearchNumber: $('#@Html.IdFor(model => model.SearchNumber)').val(),
            SearchEmail: $('#@Html.IdFor(model => model.SearchEmail)').val(),
            StatusId: $('#StatusId').val()
        };
        addAntiForgeryToken(data);
        return data;
    }
</script>